package com.st.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.st.entity.*;
import com.st.mapper.IAttractionMapper;
import com.st.mapper.ITicketMapper;
import com.st.mapper.IViewTicketMapper;
import com.st.service.IViewTicketService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class ViewTicketServiceImpl extends ServiceImpl<IViewTicketMapper, ViewTicket> implements IViewTicketService {
    @Autowired
    private IViewTicketMapper iViewTicketMapper;
    @Autowired
    private IAttractionMapper iAttractionMapper;
    @Autowired
    private ITicketMapper iTicketMapper;
    //搜索+分页
    @Override
    public PageResult<ViewTicket> search(PageDTO<ViewTicket> pageDTO) {
        if (pageDTO == null) {
            return new PageResult(false,"参数非法");
        }
        IPage<ViewTicket> iPage = new Page<>(pageDTO.getPage(),pageDTO.getLimit());
        QueryWrapper<ViewTicket> queryWrapper = new QueryWrapper<>();
        ViewTicket data = pageDTO.getData();
        if (!StringUtils.isEmpty(data.getName())){
            queryWrapper.like("name",data.getName());
        }
        if (!StringUtils.isEmpty(data.getScenicName())){
            queryWrapper.eq("scenic_name",data.getScenicName());
        }
        IPage<ViewTicket> pageInfo = iViewTicketMapper.selectPage(iPage, queryWrapper);
        long total = pageInfo.getTotal();
        long page = pageInfo.getCurrent();
        List<ViewTicket> ticketList = pageInfo.getRecords();
        return new PageResult<>(true,"数据",total,page,ticketList);
    }
    //查找所有景区
    @Override
    public Result scenicSearch() {
        QueryWrapper<Attractions> queryWrapper = new QueryWrapper<>();
        List<Attractions> attractionsList = iAttractionMapper.selectList(queryWrapper);
        return new Result(true,"景区列表",attractionsList);
    }
    //添加门票或编辑
    @Override
    public Result addTicket(Ticket ticket) {


        if (ticket.getId()==null){
            int insert = iTicketMapper.insert(ticket);
            if (insert!=0){
                return new Result(true,"添加成功");
            }
        }else {
            int i = iTicketMapper.updateById(ticket);
            if(i!=0){
                return new Result(true,"修改成功");
            }
        }
        return new Result(false,"失败了");
    }
    //编辑
    @Override
    public Result edit(Ticket ticket) {
        return new Result(false,"修改失败");
    }
    //删除
    @Override
    public Result del(Ticket ticket) {
        int i = iTicketMapper.deleteById(ticket.getId());
        if (i!=0){
            return new Result(true,"删除成功");
        }
        return new Result(true,"删除失败");
    }
    //批量删除
    @Override
    public Result delAll(List ids) {
        int i = iTicketMapper.deleteBatchIds(ids);
        if (i!=0){
            return new Result(true,"删除成功");
        }
        return new Result(true,"删除失败");
    }
}
